home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 6 / MacMania 6.toast / / Multimedia & Desktop / sk8 / SK8InJava / Code / Collections / TableCollection.java < prev    next >
Encoding:
Java Source  |  1997-02-27  |  3.0 KB  |  141 lines  |  [TEXT/CWIE]

  1. /*  SK8 © 1997 Apple Computer, Inc.
  2.     This code is protected under the current SK8 License
  3.     See http://sk8.research.apple.com/ for more information
  4.     Apple Research Laboratories
  5. */
  6.  
  7.  
  8. /* 
  9.     table
  10.     a look-up list that adheres to the SK8 collection protocol
  11. */
  12.  
  13.  
  14. public class table extends list {
  15.  
  16.     private Object mKey; 
  17.     
  18.     //constructor
  19.     public table(){
  20.         super();
  21.         mKey = null;
  22.     }
  23.     
  24.     //overriding newSibling
  25.     protected list newSibling() {
  26.         table tc = new table();
  27.         return tc;
  28.     }
  29.     
  30.     //swapValues
  31.     protected void swapValues(list inOther) {
  32.         super.swapValues(inOther);
  33.         
  34.         Object theObj = ((table)inOther).mKey;
  35.         ((table)inOther).mKey = this.mKey;        
  36.         this.mKey = theObj;
  37.     }
  38.    /**
  39.     **   collection protocol stuff 
  40.     **   inherited from List collection
  41.     **/
  42.     
  43.     
  44.    /**
  45.     **   lookup table stuff 
  46.     **/
  47.     
  48.     // keyatvisitstate
  49.     public Object keyatvisitstate(visitstate inState){
  50.         checkvisitstatetype(inState);
  51.         return ((table)((listvisitstate)inState).mCell).mKey;
  52.     };
  53.     
  54.     //visitatateatkey
  55.     public visitstate visitatateatkey(Object inKey) {
  56.         table scanner = this;
  57.         if (inKey == null) {
  58.             while ((scanner.mRest != null) && scanner.mKey != null) {
  59.                  scanner = (table)(scanner.mRest);
  60.              }
  61.              if (scanner.mKey == null) {
  62.                  return newvisitstate(this, scanner);
  63.              } else {
  64.                  return null;
  65.              }
  66.         
  67.         } else {
  68.             while ((scanner.mRest != null) && !(inKey.equals(scanner.mKey))) {
  69.                  scanner = (table)(scanner.mRest);
  70.              }
  71.              if (inKey.equals(scanner.mKey)) {
  72.                  return newvisitstate(this, scanner);
  73.              } else {
  74.                  return null;
  75.              }
  76.          }
  77.     }
  78.     
  79.     // setelementatkey
  80.     public visitstate setelementatkey(Object inKey, Object inElement) {
  81.                 
  82.         visitstate v = visitatateatkey(inKey);
  83.         
  84.         //if no existing entry, add a new one
  85.         if (v == null){
  86.             v = initialvisitstate();
  87.             v = insertatvisitstate(v, inElement);
  88.         } else {
  89.             // checkVisitStateType(v); //don't need this check, eh?
  90.             setelementatvisitstate(v, inElement);
  91.         }
  92.         //now we know that v is the correct visit state and the value has been
  93.         //correclty set.  Now we just set the key and get outta here.
  94.         listvisitstate  lvs = (listvisitstate)v;
  95.         if (lvs.mCell instanceof table) {
  96.             table tc = (table)(lvs.mCell);
  97.             tc.mKey = inKey;
  98.         } //else {
  99.             //throw new collectionexception("damn it!");}
  100.         return v;
  101.     }
  102.     
  103.     
  104.     // elementatkey
  105.     public Object elementatkey(Object inKey) {
  106.         visitstate v = visitatateatkey(inKey);
  107.         if (v == null) {
  108.             return null;
  109.         } else {
  110.             return elementatvisitstate(v);
  111.         }
  112.     }
  113.     
  114.     //helpers
  115.     public Object nth (Object n) {
  116.         return elementatkey(n);
  117.     }
  118.     public boolean setnth (Object inKey, Object inElement) {
  119.         try {
  120.             setelementatkey(inKey, inElement);
  121.             return true;
  122.         } catch (RuntimeException E) {
  123.             return false;
  124.         }
  125.     }
  126.     
  127.     
  128.         
  129.     public list keys() {
  130.         list keylist = new list();
  131.         Object i;
  132.         for (visitstate curstate = this.initialvisitstate(); (curstate != null); curstate = this.succeedingvisitstate(curstate)) {
  133.             i = this.keyatvisitstate(curstate);
  134.             keylist.pushend(i);
  135.         }
  136.         return keylist;
  137.     }
  138.  
  139.     
  140.     
  141. }